import os
import xml.dom.minidom
import cv2
from openpyxl import Workbook,load_workbook

base_PATH=os.getcwd()
PATH_images = './train/'  # 当前文件路径
PATH_annotations = os.path.abspath('./train_annotations')  # 当前文件路径

wb=load_workbook('xml.xlsx')
sn=wb.sheetnames
ws = wb[sn[0]]
ID=1
for i in range(200, 853):
    name = str(i)
    os.chdir(PATH_annotations)
    dom = xml.dom.minidom.parse(name + '.xml')
    rootdata = dom.documentElement
    item = rootdata.getElementsByTagName("object")
    for num in item:  # 一个照片中的人头数
        xmin = num.getElementsByTagName("xmin")[0]
        ymin = num.getElementsByTagName("ymin")[0]
        xmax = num.getElementsByTagName("xmax")[0]
        ymax = num.getElementsByTagName("ymax")[0]
        linshi_x = [xmin.childNodes[0].data, xmax.childNodes[0].data]
        linshi_y = [ymin.childNodes[0].data, ymax.childNodes[0].data]
        ws.append([i,xmin.childNodes[0].data,ymin.childNodes[0].data,xmax.childNodes[0].
                  data,ymax.childNodes[0].data])

    # print(len(_list_x))
    # print(mask)
#     for face in range(len(_list_x)):
#         first_point = (int(_list_x[face][0]), int(_list_y[face][0]))
#         last_point = (int(_list_x[face][1]), int(_list_y[face][1]))
#
#         cropped = image[int(_list_y[face][0]):int(_list_y[face][1]),
#                   int(_list_x[face][0]):int(_list_x[face][1])]  # 裁剪坐标为[y0:y1, x0:x1]
#
os.chdir(base_PATH)
wb.save('xml.xlsx')